﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ToolLibrary;

namespace SerialTools
{
    public partial class FrmConfig : Form
    {
        public FrmConfig()
        {
            InitializeComponent();
        }

        private void FrmConfig_Load(object sender, EventArgs e)
        {
            cbxAutoOpenOnStart.Checked = Program.Config.AutoOpenOnStart;
            cbxDataBits.Text = Program.Config.DataBits.ToString();
            cbxParity.Text = Program.Config.Parity.ToString();
            cbxStopBits.Text = Program.Config.StopBits.ToString();
            cbxHandshake.Text = Program.Config.Handshake.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Program.Config.AutoOpenOnStart = cbxAutoOpenOnStart.Checked;
            Program.Config.DataBits = cbxDataBits.Text.ToInt();

            if (cbxParity.Text.Contains("Odd"))
                Program.Config.Parity = Parity.Odd;
            else if (cbxParity.Text.Contains("Even"))
                Program.Config.Parity = Parity.Even;
            else if (cbxParity.Text.Contains("Space"))
                Program.Config.Parity = Parity.Space;
            else if (cbxParity.Text.Contains("Mark"))
                Program.Config.Parity = Parity.Mark;

            if (cbxStopBits.Text.Equals("One"))
                Program.Config.StopBits = StopBits.One;
            else if (cbxStopBits.Text.Equals("OnePointFive"))
                Program.Config.StopBits = StopBits.OnePointFive;
            else if (cbxStopBits.Text.Equals("Two"))
                Program.Config.StopBits = StopBits.Two;

            if (cbxHandshake.Text.Equals("None"))
                Program.Config.Handshake = Handshake.None;
            else if (cbxStopBits.Text.Equals("XOnXOff"))
                Program.Config.Handshake = Handshake.XOnXOff;
            else if (cbxStopBits.Text.Equals("RequestToSend"))
                Program.Config.Handshake = Handshake.RequestToSend;
            else if (cbxStopBits.Text.Equals("RequestToSendXOnXOff"))
                Program.Config.Handshake = Handshake.RequestToSendXOnXOff;

            //写配置文件
            XmlHelper.XmlSerializeToFile(Program.Config, Program.ConfigPath);
            MessageBox.Show(this, "保存成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
